
import newsStore from './newsStore'
import userStore from './userStore'

import {ComponentCustomProperties} from 'vue'
import {Store,createStore} from 'vuex'

// 配置vue+ts项目里面使用vuex
declare module '@vue/runtime-core' {
    interface State{
        count: number,
        list:string[],
        msg:string
    }
    interface ComponentCustomProperties{
        $store:Store<State>
    }
}

const store = createStore({
    state(){  //数据
        return{
            count:1,
            list:['马','鸡'],
            msg:'你好vue'
        }
    },
    mutations:{   //方法  
        incCount(state:any){
                state.count++
   
        },
        setCount(state:any,num:number){
            state.count = num
        },
        setMsg(state:any,msg:string){
            state.msg = msg
        }
    },
    getters:{ //计算属性
        reverseMsg(state:any){
            return state.msg.split("").reverse().join()
        },
        num(state:any){
            return state.count+10
        }
    },
    actions:{  //执行mutations里面的方法  异步操作放在actions
        incCount(context){
            context.commit("incCount")//执行mutations incCount
        },
        setCount({commit},num){
            commit("setCount",num)
        },
        setMsg(context,msg:string){
            setTimeout(()=>{
                context.commit("setMsg",msg)  //执行mutations setMsg
            },1000)
        }
    },
    modules:{
        "user":userStore,
        "news":newsStore
    }
})
export default store